package com.hand.control;

import com.hand.Dao.userDao;
import com.hand.domain.User;
import com.hand.domain.UserRole;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
public class FindByRoleName {

    @RequestMapping("/findByRoleName")
    public List<UserRole> findAll() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userDao mapper = sqlSession.getMapper(userDao.class);

        //根据角色名字查询
        UserRole user3 = new UserRole();
        user3.setRole_name("司令");
        List<UserRole> list1 = mapper.findByRoleName(user3);
        System.out.println(list1);

        sqlSession.commit();
        sqlSession.close();
        return list1;
    }
}
